﻿create procedure [dbo].[usp_LogEntryInsert]
	@PublicationId uniqueidentifier
	, @AppDomainId int
	, @AssemblyId int
	, @MachineId int
	, @IIdentityId int
	, @ProcessId int
	, @ThreadId int
	, @WorkItemId int
	, @LogEntry nvarchar(max) 
	, @Priority int
	, @Type int
        , @SerializedDictionaryXml xml
	, @XElement xml 
as

begin try
	
	begin transaction
	
		if not exists(select * from [dbo].[LogEntry] where [PublicationId] = @PublicationId and
		                                                   [AppDomainId] = @AppDomainId and
		                                                   [AssemblyId] = @AssemblyId and
		                                                   ([MachineId] = @MachineId or ([MachineId] is null and @MachineId is null)) and
		                                                   [IIdentityId] = @IIdentityId and
													       ([ProcessId] = @ProcessId or ([ProcessId] is null and @ProcessId is null)) and
		                                                   [ThreadId] = @ThreadId and
		                                                   ([WorkItemId] = @WorkItemId or ([WorkItemId] is null and @WorkItemId is null)) and
		                                                   [LogEntry] = @LogEntry and
		                                                   [Priority] = @Priority and
		                                                   [Type] = @Type and
		                                                   [dbo].[CompareXml]([SerializedDictionaryXml], @SerializedDictionaryXml) = 0 and
		                                                   [dbo].[CompareXml]([XElement], @XElement) = 0)
			begin
										                              
				insert [dbo].[LogEntry]
				(
					[PublicationId]
					, [AppDomainId]
					, [AssemblyId]
					, [MachineId]
					, [IIdentityId]
					, [ProcessId]
					, [ThreadId]
					, [WorkItemId]
					, [LogEntry] 
					, [Priority]
					, [Type]
					, [SerializedDictionaryXml]
					, [XElement]
				)
				values
				(
					@PublicationId
					, @AppDomainId
					, @AssemblyId
					, @MachineId
					, @IIdentityId
					, @ProcessId
					, @ThreadId
					, @WorkItemId
					, @LogEntry 
					, @Priority
					, @Type
					, @SerializedDictionaryXml
					, @XElement
				)
				
				select [LogEntryId], [ConcurrencyId], [PublicationId], [Created], [Updated] from [dbo].[LogEntry] where @@rowcount > 0 and [LogEntryId] = scope_identity()
			
			end
			
		else
			
			begin
			
				select [LogEntryId], [ConcurrencyId], [PublicationId], [Created], [Updated] from [dbo].[LogEntry] where [PublicationId] = @PublicationId and
				                                                                                                        [AppDomainId] = @AppDomainId and
																										                [AssemblyId] = @AssemblyId and
																										                ([MachineId] = @MachineId or ([MachineId] is null and @MachineId is null)) and
																										                [IIdentityId] = @IIdentityId and
															                                                            ([ProcessId] = @ProcessId or ([ProcessId] is null and @ProcessId is null)) and
																										                [ThreadId] = @ThreadId and
																										                ([WorkItemId] = @WorkItemId or ([WorkItemId] is null and @WorkItemId is null)) and
																										                [LogEntry] = @LogEntry and
																										                [Priority] = @Priority and
																										                [Type] = @Type and
																										                [dbo].[CompareXml]([SerializedDictionaryXml], @SerializedDictionaryXml) = 0 and
																										                [dbo].[CompareXml]([XElement], @XElement) = 0
			end
	
	commit transaction

end try
	
begin catch
	if @@trancount > 0
	begin
		rollback transaction;
	end
	execute [dbo].[usp_SqlErrorInsert]
end catch

